import 'package:ducafe_ui_core/ducafe_ui_core.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'index.dart';

class ColorsPage extends GetView<ColorsController> {
  const ColorsPage({super.key});

// 将 ColorScheme 映射为 Map<String, Color>
  Map<String, Color> getColorMap(ColorScheme scheme) {
    return {
      'primary': scheme.primary,
      'surfaceTint': scheme.surfaceTint,
      'onPrimary': scheme.onPrimary,
      'primaryContainer': scheme.primaryContainer,
      'onPrimaryContainer': scheme.onPrimaryContainer,
      'secondary': scheme.secondary,
      'onSecondary': scheme.onSecondary,
      'secondaryContainer': scheme.secondaryContainer,
      'onSecondaryContainer': scheme.onSecondaryContainer,
      'tertiary': scheme.tertiary,
      'onTertiary': scheme.onTertiary,
      'tertiaryContainer': scheme.tertiaryContainer,
      'onTertiaryContainer': scheme.onTertiaryContainer,
      'error': scheme.error,
      'onError': scheme.onError,
      'errorContainer': scheme.errorContainer,
      'onErrorContainer': scheme.onErrorContainer,
      'surface': scheme.surface,
      'onSurface': scheme.onSurface,
      'onSurfaceVariant': scheme.onSurfaceVariant,
      'outline': scheme.outline,
      'outlineVariant': scheme.outlineVariant,
      'shadow': scheme.shadow,
      'scrim': scheme.scrim,
      'inverseSurface': scheme.inverseSurface,
      'inversePrimary': scheme.inversePrimary,
      'primaryFixed': scheme.primaryFixed,
      'onPrimaryFixed': scheme.onPrimaryFixed,
      'primaryFixedDim': scheme.primaryFixedDim,
      'onPrimaryFixedVariant': scheme.onPrimaryFixedVariant,
      'secondaryFixed': scheme.secondaryFixed,
      'onSecondaryFixed': scheme.onSecondaryFixed,
      'secondaryFixedDim': scheme.secondaryFixedDim,
      'onSecondaryFixedVariant': scheme.onSecondaryFixedVariant,
      'tertiaryFixed': scheme.tertiaryFixed,
      'onTertiaryFixed': scheme.onTertiaryFixed,
      'tertiaryFixedDim': scheme.tertiaryFixedDim,
      'onTertiaryFixedVariant': scheme.onTertiaryFixedVariant,
      'surfaceDim': scheme.surfaceDim,
      'surfaceBright': scheme.surfaceBright,
      'surfaceContainerLowest': scheme.surfaceContainerLowest,
      'surfaceContainerLow': scheme.surfaceContainerLow,
      'surfaceContainer': scheme.surfaceContainer,
      'surfaceContainerHigh': scheme.surfaceContainerHigh,
      'surfaceContainerHighest': scheme.surfaceContainerHighest,
    };
  }

  // 将 ThemeColorsExtensions 映射为 Map<String, Color>
  Map<String, Color> getThemeColorsExtensionsMap(BuildContext context) {
    var colors = context.colors;

    return {
      'primary': colors.primary,
      'primaryLight': colors.primaryLight,
      'primaryDark': colors.primaryDark,
      'canvas': colors.canvas,
      'scaffoldBackground': colors.scaffoldBackground,
      'card': colors.card,
      'divider': colors.divider,
      'focus': colors.focus,
      'hover': colors.hover,
      'highlight': colors.highlight,
      'splash': colors.splash,
      'unselectedWidget': colors.unselectedWidget,
      'disabled': colors.disabled,
      'secondaryHeader': colors.secondaryHeader,
      'dialogBackground': colors.dialogBackground,
      'indicator': colors.indicator,
      'hint': colors.hint,
      'shadow': colors.shadow,
    };
  }

  // 提取通用的列表构建方法
Widget _buildColorListFromMap(Map<String, Color> colorMap, String title) {
  final List<Map<String, Object>> colorEntries = colorMap.entries
      .map((entry) => {'name': entry.key, 'color': entry.value})
      .toList();

  return Column(
    children: [
      Padding(
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        child: Text(
          title,
          style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
        ),
      ),
      ListView.builder(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        itemCount: colorEntries.length,
        itemBuilder: (context, index) {
          final entry = colorEntries[index];
          return Padding(
            padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Container(
                  width: 40,
                  height: 40,
                  color: entry['color'] as Color,
                  margin: const EdgeInsets.only(right: 16),
                ),
                Text(entry['name']! as String),
              ],
            ),
          );
        },
      ),
    ],
  );
}

// 主视图：展示两个列表
Widget _buildView(BuildContext context) {
  final colorMap1 = getColorMap(context.colors.scheme);
  final colorMap2 = getThemeColorsExtensionsMap(context);

  return ListView(
    padding: EdgeInsets.zero,
    children: [
      _buildColorListFromMap(colorMap1, 'Color Scheme Colors'),
      _buildColorListFromMap(colorMap2, 'Theme Colors (Extended)'),
    ],
  );
}

  @override
  Widget build(BuildContext context) {
    return GetBuilder<ColorsController>(
      init: ColorsController(),
      id: "colors",
      builder: (_) {
        return Scaffold(
          appBar: AppBar(title: const Text("colors")),
          body: SafeArea(
            child: _buildView(context),
          ),
        );
      },
    );
  }
}
